package com.zh.rent.util;

import com.zh.rent.dao.AlipayConfigDao;
import com.zh.rent.pojo.AlipayConfig;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ExpireTime {
    /**
           * 指定日期加上天数后的日期
           * @param num 为增加的天数
           * @param newDate 创建时间
           * @return
          */
 public static String plusDay(int num,String newDate)  {
     try {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         Date date=sdf.parse(newDate);
         String dateFormat1=sdf.format(date);
        // System.out.println("需要增加的时间："+dateFormat1);
         Calendar calendar=Calendar.getInstance();
         calendar.setTime(date);
         calendar.add(Calendar.DATE, num);
         Date date2=calendar.getTime();
         String dateFormat=sdf.format(date2);
         System.out.println("过期时间："+dateFormat);
         return dateFormat;
             } catch (ParseException e) {
                 return "计算过期时间失败";
                 }

            }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stamp = "";
        if (!"".equals(time)) {//时间不为空
            try {
                stamp = String.valueOf(sdf.parse(time).getTime()/1000);
            } catch (Exception e) {
                System.out.println("参数为空！");
            }
        }else {    //时间为空
            long current_time = System.currentTimeMillis();  //获取当前时间
            stamp = String.valueOf(current_time/1000);
        }
        return stamp;
    }

}
